using System;
using System.Windows.Forms;
using WindowsLive.Writer.Api;
using WindowsLive.Writer.Extensibility.BlogClient;


namespace WLWCreateCalendarItemPlugin
{
    [WriterPlugin("c8538481-1ba1-4873-a558-1afef59fe36e", "Add to Outlook Calendar",
        PublisherUrl = "http://blogs.infinite-x.net",
        Description = "Creates an Outlook Calendar appointment for a future blog post",
        ImagePath = "writer.png",
        HasEditableOptions = true)]
    public class Plugin : PublishNotificationHook
    {
        Settings PluginSettings;

        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);

            PluginSettings = new Settings(pluginOptions);
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (var op = new FormOptions(PluginSettings))
            {
                op.ShowDialog();
            }
        }

        public override void OnPostPublish(IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
        {
            base.OnPostPublish(dialogOwner, properties, publishingContext, publish);

            //
            // This may be a bit unsupported but it is the only way to get the publish date.
            //
            var newPost = (BlogPost)publishingContext.PostInfo;

            //
            // If we are publishing an article today, then there is not need
            // to create an appointment.
            //
            if (newPost.DatePublished.Date == DateTime.Today.Date)
            {
                return;
            }

            var articleTitle = string.Format("Article: {0}", publishingContext.PostInfo.Title);

            OutlookUtility.CreateOutlookAppointment(PluginSettings.CalendarFolderName, articleTitle, publishingContext.BlogName, newPost.DatePublished);
        }
    }
}
